﻿using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// EditStyle 的摘要说明。
    /// </summary>
    public partial class EditStylePage : Page
    {
        private int _styleId;

        protected override void OnInit(EventArgs e)
        {
            cmdOK.Click += cmdOK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            _styleId = QSUtil.GetPositiveInt32("StyleID");
            if (_styleId > 0)
            {
                if (!Page.IsPostBack)
                {
                    Bangding();
                }
            }
            else
            {
                oldTemplateRow.Visible = false;
                cbKeepOldFile.Visible = false;
            }

            base.OnLoad(e);
        }

        private void Bangding()
        {
            FlowStyle style = FlowStyle.GetFlowStyle(_styleId);
            if (style != null)
            {
                txtStyleName.Text = style.Name;
                txtStyleRemark.Text = style.Description;

                oldTemplateRow.Visible = !string.IsNullOrEmpty(style.Template);
                if (oldTemplateRow.Visible)
                {
                    lblTemplate.Text = "<a href=\"" + HttpUtility.HtmlAttributeEncode(style.TemplateFullVirtualPath) + "\">" + HttpUtility.HtmlEncode(style.Template) + "</a>";
                }
            }
            else
            {
                // The style does not exist in DB.
                oldTemplateRow.Visible = false;
                cbKeepOldFile.Visible = false;
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            FlowStyle style;
            if (_styleId == 0)
            {
                style = FlowStyle.AddStyle(txtStyleName.Text, txtStyleRemark.Text, fFileTemplate.PostedFile);
            }
            else
            {
                style = FlowStyle.UpdateStyle(_styleId, txtStyleName.Text, txtStyleRemark.Text, fFileTemplate.PostedFile, !cbKeepOldFile.Checked);
            }

            Response.Redirect("DefineStyle.aspx?StyleID=" + style.ID);
        }
    }
}